/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
http://java-all-frameworks.blogspot.com.ar/2011/04/este-sitio-esta-en-construccion.html
 */
package ventanas;

import controlador.CtrlVista;
import java.util.Iterator;
import javax.swing.JOptionPane;
import negocio.Butaca;
import negocio.Fila;
import negocio.Sala;
import negocio.Seccion;
import utilidades.OrdListModel;

/**
 *
 * @author Diego
 */
public class FrmButaca extends javax.swing.JDialog {
    private CtrlVista vista;// = new CtrlVista();
    private Butaca unaButaca;
    private Sala unaSala;
    private Seccion unaSeccion;
    private Fila unaFila;
    OrdListModel modelSalas = new OrdListModel();
    OrdListModel modelSecciones = new OrdListModel();
    OrdListModel modelFilas = new OrdListModel();
    OrdListModel modelButacas = new OrdListModel();
    /**
     * Creates new form Butacas
     */
    public FrmButaca(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        
        try{
            this.vista = new CtrlVista();
        }
        catch(Exception e){            
            JOptionPane.showMessageDialog(this, "Error de Inicializacion");
        }
        this.inicializar();
    }
    
    public void inicializar(){
        this.lstSalas.setModel(modelSalas);
        this.lstSecciones.setModel(modelSecciones);
        this.lstFilas.setModel(modelFilas);
        this.lstButacas.setModel(modelButacas);
        this.txtNro.setEnabled(false);
        this.txtNro.setText("");
        this.rdbDeshabilitada.setEnabled(false);
        this.rdbHabilitada.setEnabled(false);
        this.btnNuevo.setEnabled(true);
        this.btnGuardar.setEnabled(false);
        this.rdbDeshabilitada.setSelected(false);
        this.rdbHabilitada.setSelected(false);
        this.cargarSalas();
    }
    public void cargarSalas(){
        this.modelSalas.clear();
        this.lstSalas.setModel(modelSalas);
        if(this.vista.obtenerSalasActivas().size() > 0){
            Iterator it = this.vista.obtenerSalasActivas().iterator();
            while(it.hasNext()){
                unaSala = (Sala) it.next();
                this.modelSalas.addElement(unaSala);
            }
            unaSala = null;
        }else{
            this.modelSalas.addElement("No Se encontraron Salas");
        }
    }
    public void cargarSecciones(){
        this.modelSecciones.clear();
        this.modelFilas.clear();
        this.modelButacas.clear();
        this.lstSecciones.setModel(modelSecciones);
        if(this.unaSala.ObtenerSeccionesActivas().size() > 0){
            Iterator it = this.unaSala.ObtenerSeccionesActivas().iterator();
            while(it.hasNext()){
                Seccion sec = (Seccion) it.next();
                this.modelSecciones.addElement(sec);
            }
        }else{
            this.modelSecciones.addElement("No se encontraron secciones");
        }
        JOptionPane.showMessageDialog(this, this.unaSala.ObtenerSeccionesActivas().size());
    }
    public void cargarFilas(){
        this.modelFilas.clear();
        this.modelButacas.clear();
        this.lstFilas.setModel(modelFilas);
        if(this.unaSeccion.obtenerFilasActivas().size() > 0){
            Iterator it = this.unaSeccion.obtenerFilasActivas().iterator();
            while(it.hasNext()){
                Fila fil = (Fila) it.next();
                this.modelFilas.addElement(fil);
            }
        }else{
            this.modelFilas.addElement("No se encontraron Filas");
        }
    }
    public void cargarButacas(){
        this.modelButacas.clear();
        this.lstButacas.setModel(modelButacas);
        if(this.unaFila.getButacas().size() > 0){
            Iterator it = this.unaFila.getButacas().iterator();
            while(it.hasNext()){
                Butaca but = (Butaca) it.next();
                this.modelButacas.addElement(but);
            }
        }else{
            this.modelButacas.addElement("No se encontraron Butacas");
        }
    }
    public void seleccionarSala(){
        this.unaSala = (Sala) this.modelSalas.getElementAt(this.lstSalas.getSelectedIndex());
        //this.cargarSecciones();
    }
    public void seleccionarSeccion(){
        this.unaSeccion = (Seccion) this.modelSecciones.getElementAt(this.lstSecciones.getSelectedIndex());
        this.cargarFilas();
    }
    public void seleccionarFila(){
        this.unaFila = (Fila) this.modelFilas.getElementAt(this.lstFilas.getSelectedIndex());
        this.cargarButacas();
    }
    public void seleccionarButaca(){
        this.unaButaca = (Butaca) this.modelButacas.getElementAt(this.lstButacas.getSelectedIndex());
        this.cargarCampos();
    }
    public void cargarCampos(){
        this.txtNro.setEnabled(true);
        this.txtNro.setText(this.unaButaca.getNro());
        this.rdbHabilitada.setEnabled(true);
        this.rdbDeshabilitada.setEnabled(true);
        if(this.unaButaca.isEstado()){
            this.rdbHabilitada.setSelected(true);
            this.rdbDeshabilitada.setSelected(false);
        }else{
            this.rdbHabilitada.setSelected(false);
            this.rdbDeshabilitada.setSelected(false);
        }
    }
    public void nuevoRegistro(){
        this.unaButaca = null;
        this.txtNro.setEnabled(true);
        this.rdbDeshabilitada.setEnabled(true);
        this.rdbHabilitada.setEnabled(true);
        this.rdbDeshabilitada.setSelected(false);
        this.rdbHabilitada.setSelected(true);
        this.btnNuevo.setEnabled(false);
        this.btnGuardar.setEnabled(true);
        
    }
    public void guardar(){
        if(this.unaButaca == null){
            //Butaca butaca = new Butaca(this.txtNro,true);
            String nro = this.txtNro.getText();
            boolean estado = true;
            if(this.rdbHabilitada.isSelected()){
                estado = true;
            }else{
                estado = false;
            }
            try{
                this.vista.agregarButaca(this.unaFila,nro,estado);
            }
            catch (Exception e){
                System.out.println(e.getMessage());
            }
        }
        this.cargarButacas();
        this.inicilizarBototnes();
    }
    public void inicilizarBototnes(){
        this.txtNro.setText("");
        this.txtNro.setEnabled(false);
        this.btnNuevo.setEnabled(true);
        this.btnGuardar.setEnabled(false);
        this.unaButaca = null;
        this.rdbDeshabilitada.setSelected(false);
        this.rdbDeshabilitada.setEnabled(false);
        this.rdbHabilitada.setEnabled(false);
        this.rdbDeshabilitada.setSelected(false);        
    }
    

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        lstSalas = new javax.swing.JList();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        lstSecciones = new javax.swing.JList();
        jLabel3 = new javax.swing.JLabel();
        jScrollPane3 = new javax.swing.JScrollPane();
        lstFilas = new javax.swing.JList();
        jLabel4 = new javax.swing.JLabel();
        jScrollPane4 = new javax.swing.JScrollPane();
        lstButacas = new javax.swing.JList();
        jLabel5 = new javax.swing.JLabel();
        txtNro = new javax.swing.JTextField();
        rdbHabilitada = new javax.swing.JRadioButton();
        rdbDeshabilitada = new javax.swing.JRadioButton();
        btnNuevo = new javax.swing.JButton();
        btnGuardar = new javax.swing.JButton();
        btnAceptar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setText("Sala");

        lstSalas.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        lstSalas.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lstSalasMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(lstSalas);

        jLabel2.setText("Seccion");

        lstSecciones.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        lstSecciones.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lstSeccionesMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(lstSecciones);

        jLabel3.setText("Fila");

        lstFilas.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        lstFilas.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lstFilasMouseClicked(evt);
            }
        });
        jScrollPane3.setViewportView(lstFilas);

        jLabel4.setText("Butacas");

        lstButacas.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        lstButacas.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lstButacasMouseClicked(evt);
            }
        });
        jScrollPane4.setViewportView(lstButacas);

        jLabel5.setText("Nro");

        rdbHabilitada.setText("Habilitada");

        rdbDeshabilitada.setText("Deshabilitada");

        btnNuevo.setText("Nueva");
        btnNuevo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNuevoActionPerformed(evt);
            }
        });

        btnGuardar.setText("Guardar");
        btnGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGuardarActionPerformed(evt);
            }
        });

        btnAceptar.setText("Aceptar");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(179, 179, 179)
                        .addComponent(jLabel2)
                        .addGap(156, 156, 156)
                        .addComponent(jLabel3)
                        .addGap(155, 155, 155)
                        .addComponent(jLabel4))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 184, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 176, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 163, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 147, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel5)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(txtNro, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(rdbHabilitada)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(rdbDeshabilitada))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(btnNuevo)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btnGuardar)))))
                .addContainerGap(18, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(btnAceptar)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 402, Short.MAX_VALUE)
                                .addComponent(jScrollPane2)
                                .addComponent(jScrollPane3)
                                .addComponent(jScrollPane4))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel5)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(rdbHabilitada)
                                    .addComponent(rdbDeshabilitada))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(btnNuevo)
                                    .addComponent(btnGuardar)))))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(txtNro, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(388, 388, 388)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(btnAceptar)
                .addGap(19, 19, 19))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void lstSalasMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lstSalasMouseClicked
        // TODO add your handling code here:
        this.seleccionarSala();
        this.cargarSecciones();
    }//GEN-LAST:event_lstSalasMouseClicked

    private void lstSeccionesMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lstSeccionesMouseClicked
        // TODO add your handling code here:
        this.seleccionarSeccion();
        this.cargarFilas();
    }//GEN-LAST:event_lstSeccionesMouseClicked

    private void lstFilasMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lstFilasMouseClicked
        // TODO add your handling code here:
        this.seleccionarFila();
        this.cargarButacas();
    }//GEN-LAST:event_lstFilasMouseClicked

    private void lstButacasMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lstButacasMouseClicked
        // TODO add your handling code here:
        this.seleccionarButaca();
    }//GEN-LAST:event_lstButacasMouseClicked

    private void btnNuevoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNuevoActionPerformed
        // TODO add your handling code here:
        this.nuevoRegistro();
    }//GEN-LAST:event_btnNuevoActionPerformed

    private void btnGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGuardarActionPerformed
        // TODO add your handling code here:
        this.guardar();
    }//GEN-LAST:event_btnGuardarActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main2(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(FrmButaca.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(FrmButaca.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(FrmButaca.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(FrmButaca.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
        //</editor-fold>
        //</editor-fold>
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                FrmButaca dialog = new FrmButaca(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAceptar;
    private javax.swing.JButton btnGuardar;
    private javax.swing.JButton btnNuevo;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JList lstButacas;
    private javax.swing.JList lstFilas;
    private javax.swing.JList lstSalas;
    private javax.swing.JList lstSecciones;
    private javax.swing.JRadioButton rdbDeshabilitada;
    private javax.swing.JRadioButton rdbHabilitada;
    private javax.swing.JTextField txtNro;
    // End of variables declaration//GEN-END:variables
}
